package cn.wen.java.lang;

/**
 * Title：布尔辅助类
 * <p>
 * Description：
 * <p>
 * Copyright：Copyright (c) 2010-2012
 * 
 * @author <a href="mailto:ct_wen@qq.com">ctwen</a>, 温承瑭
 * @version Revision：1.0 Date：2011/2/13 Description：初完成。
 *          <p>
 * @see org.apache.commons.lang.BooleanUtils
 */
public abstract class HelperOfBoolean {

    // Integer to Boolean methods
    // -----------------------------------------------------------------------
    /**
     * Converts an Integer to a Boolean using the convention that <code>zero</code> is <code>false</code>.
     * <p>
     * <code>null</code> will be converted to <code>null</code>.
     * 
     * <pre>
     *   HelperOfBoolean.toBoolean(new Integer(0))    = Boolean.FALSE
     *   HelperOfBoolean.toBoolean(new Integer(1))    = Boolean.TRUE
     *   HelperOfBoolean.toBoolean(new Integer(null)) = null
     * </pre>
     * 
     * @param value the Integer to convert
     * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, <code>null</code> if <code>null</code> input
     */
    public static Boolean toBoolean(Integer value) {
	if (value == null) {
	    return null;
	}
	return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
    }

    // Boolean to Integer methods
    // -----------------------------------------------------------------------
    /**
     * Converts a Boolean to a Integer using the convention that <code>zero</code> is <code>false</code>.
     * <p>
     * <code>null</code> will be converted to <code>null</code>.
     * 
     * <pre>
     *   HelperOfBoolean.toInteger(Boolean.TRUE)  = new Integer(1)
     *   HelperOfBoolean.toInteger(Boolean.FALSE) = new Integer(0)
     * </pre>
     * 
     * @param bool the Boolean to convert
     * @return one if Boolean.TRUE, zero if Boolean.FALSE, <code>null</code> if <code>null</code>
     */
    public static Integer toInteger(Boolean bool) {
	if (bool == null) {
	    return null;
	}
	return bool.booleanValue() ? 1 : 0;
    }

    // String to Boolean methods
    // -----------------------------------------------------------------------
    /**
     * Converts a String to a Boolean.
     * <p>
     * <code>'true'</code>, <code>'on'</code> or <code>'yes'</code> (case insensitive) will return <code>true</code>.
     * <code>'false'</code>, <code>'off'</code> or <code>'no'</code> (case insensitive) will return <code>false</code>.
     * Otherwise, <code>false</code> is returned.
     * 
     * <pre>
     *   HelperOfBoolean.toBoolean(null)    = null
     *   HelperOfBoolean.toBoolean("true")  = Boolean.TRUE
     *   HelperOfBoolean.toBoolean("false") = Boolean.FALSE
     *   HelperOfBoolean.toBoolean("on")    = Boolean.TRUE
     *   HelperOfBoolean.toBoolean("ON")    = Boolean.TRUE
     *   HelperOfBoolean.toBoolean("off")   = Boolean.FALSE
     *   HelperOfBoolean.toBoolean("oFf")   = Boolean.FALSE
     *   HelperOfBoolean.toBoolean("blue")  = Boolean.FALSE
     * </pre>
     * 
     * @param str the String to check
     * @return the Boolean value of the string, <code>false</code> if no match
     */
    public static Boolean toBoolean(String str) {
	if ("true".equalsIgnoreCase(str)) {
	    return Boolean.TRUE;
	}
	else if ("false".equalsIgnoreCase(str)) {
	    return Boolean.FALSE;
	}
	else if ("on".equalsIgnoreCase(str)) {
	    return Boolean.TRUE;
	}
	else if ("off".equalsIgnoreCase(str)) {
	    return Boolean.FALSE;
	}
	else if ("yes".equalsIgnoreCase(str)) {
	    return Boolean.TRUE;
	}
	else if ("no".equalsIgnoreCase(str)) {
	    return Boolean.FALSE;
	}
	// no match
	return Boolean.FALSE;
    }

}
